package com.example.parkdemo.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.base.mvvm.BaseActivity
import com.example.base.user.RepairBean
import com.example.parkdemo.R
import com.example.parkdemo.adapter.RepairAdapter
import com.example.parkdemo.adapter.RepairUserAdapter
import com.example.parkdemo.databinding.ActivityRepairBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding, RepairViewModel>() {
    lateinit var repairAdapter: RepairAdapter
    private var isAllSelect = false
    lateinit var repairUserAdapter: RepairUserAdapter
    override fun initData() {
        vm.getAllSuccess.observe(this) {
            val list = Gson().fromJson(it, RepairBean::class.java)
            repairAdapter.data.clear()
            repairAdapter.data.addAll(list.reversed())
            repairAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getAll()
    }

    override fun initView() {
        binding.repairHeader.headerTitle.text = "维修管理"
        binding.repairHeader.headerBack.setOnClickListener { finish() }
        binding.repairHeader.headerRepair.visibility = View.VISIBLE
        binding.repairHeader.headerRepair.setOnClickListener {
            if (binding.repairHeader.headerRepair.text == "编辑") {
                binding.repairHeader.headerRepair.setText("完成")
                showAdd(true)
                binding.repairLl.visibility = View.VISIBLE
                repairUserAdapter.data.clear()
                repairUserAdapter.data.addAll(arrayListOf("张师傅", "王师傅", "刘师傅", "叶师傅"))
                repairUserAdapter.notifyDataSetChanged()
            } else {
                binding.repairHeader.headerRepair.setText("编辑")
                showAdd(false)
                binding.repairLl.visibility = View.GONE

            }
        }
        repairUserAdapter = RepairUserAdapter()
        binding.repairUserRv.adapter = repairUserAdapter
        binding.repairUserRv.layoutManager = LinearLayoutManager(this)

        repairAdapter = RepairAdapter()
        binding.repairRv.adapter = repairAdapter
        binding.repairRv.layoutManager = LinearLayoutManager(this)

        repairUserAdapter.setOnItemClickListener { adapter, view, position ->

            repairUserAdapter.getViewByPosition(position, R.id.item_repair_user_tv)
                ?.setBackgroundResource(R.drawable.repair_bg)
        }

        repairAdapter.setOnItemClickListener { adapter, view, position ->
            if (repairAdapter.data[position].isShow) {
                repairAdapter.data[position].isSelected = !repairAdapter.data[position].isSelected
                repairAdapter.notifyDataSetChanged()
            }

        }
        binding.repairSelectAllIv.setOnClickListener {
            if (isAllSelect) {
                isAllSelect = false
                binding.repairSelectAllIv.setImageResource(R.drawable.gou_default)
                selectAll(isAllSelect)
            } else {
                isAllSelect = true
                binding.repairSelectAllIv.setImageResource(R.drawable.gou)
                selectAll(isAllSelect)
            }

        }


    }

    private fun seledeleteAll() {
        val list = mutableListOf<Boolean>()
        repairAdapter.data.forEach {
            list.add(it.isSelected)
        }
        binding.repairSelectAllIv.isClickable = false !in list
    }

    private fun selectAll(allSelect: Boolean) {
        repairAdapter.data.forEach {
            it.isSelected = allSelect
        }
        repairAdapter.notifyDataSetChanged()
    }

    private fun showAdd(b: Boolean) {
        repairAdapter.data.forEach {
            it.isShow = b
        }
        repairAdapter.notifyDataSetChanged()
    }

}